const ActivitiesService = require('../services/activitiesService');

// 获取所有可参与的旅游活动信息
exports.getActivitiesList = async (ctx) => {
  try {
    const { scenic_id, category, page, limit } = ctx.query;
    const activities = await ActivitiesService.getActivitiesList(scenic_id, category, page, limit);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: activities
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 获取单个旅游活动的详细信息
exports.getActivityById = async (ctx) => {
  try {
    const { id } = ctx.params;
    const activity = await ActivitiesService.getActivityById(id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: activity
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 创建一个新的旅游活动
exports.createActivity = async (ctx) => {
  try {
    const activityData = ctx.request.body;
    const activity = await ActivitiesService.createActivity(activityData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: "活动创建成功",
      data: { activity_id: activity.id }
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "创建失败",
      error: error.message
    };
  }
};

// 用户报名参加某个活动
exports.signupForActivity = async (ctx) => {
  try {
    const signupData = ctx.request.body;
    const signup = await ActivitiesService.signupForActivity(signupData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: "报名成功",
      data: { signup_id: signup.signup_id }
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "报名失败",
      error: error.message
    };
  }
};

// 查询用户报名的所有活动记录
exports.getUserSignups = async (ctx) => {
  try {
    const { user_id } = ctx.params;
    const signups = await ActivitiesService.getUserSignups(user_id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: signups
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 用户取消已报名的活动
exports.cancelSignup = async (ctx) => {
  try {
    const { signup_id, user_id } = ctx.request.body;
    const result = await ActivitiesService.cancelSignup(signup_id, user_id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: result.status
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "取消报名失败",
      error: error.message
    };
  }
};

// 管理员修改已发布的活动信息
exports.updateActivity = async (ctx) => {
  try {
    const { id } = ctx.params;
    const activityData = ctx.request.body;
    const result = await ActivitiesService.updateActivity(id, activityData);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: result.message,
      data: result
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "更新失败",
      error: error.message
    };
  }
};
